package com.hualude.roadsign.repository;

import com.hualude.roadsign.dto.*;
import com.hualude.roadsign.model.CoverFaultEntity;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

import java.util.List;

/**
 * Created by pjn on 2017/8/11.
 */
public interface CoverFaultRepository extends CrudRepository<CoverFaultEntity, Long> {
    /**
     * @Description: 获取窨井datatable
     * @Author: pan-jianan
     * @Date: 2017/8/11 17:20
     */
    public Integer getCoverReportFaultCount4DT(Integer actorId, Integer userId, Integer orgId, String strSearch) throws Exception;

    public List<DTCoverFaultDto> getCoverReportFault4DT(DTRequestDto dtRequestDto, Integer actorId, Integer userId, Integer orgId) throws Exception;


    /**
     * @Description: 窨井故障详情
     * @Author: pan-jianan
     * @Date: 2017/8/11 17:20
     */
    @Query(value = "select new com.hualude.roadsign.dto.CoverFaultDto(cf.coverCheck,cf.coverMainCheck,cf.coverFeedback," +
            "cf.coverMoney,cf.coverFaultLevel,cf.coverFaultPhoto,cf.coverFaultType,cf.coverFaultReason,cf.coverFaultCreatorId," +
            "cf.coverFaultTime) " +
            "from CoverFaultEntity cf where cf.coverFaultId=?1")
    public CoverFaultDto getCoverFaultInfo2Fault(Integer coverFaultId) throws Exception;

    /**
     * @Description: 更新故障
     * @Author: pan-jianan
     * @Date: 2017/8/11 17:20
     */
    @Modifying
    @Query(value = "UPDATE CoverFaultEntity  cf SET cf.coverFaultLevel = ?2,cf.coverFaultType = ?3,cf.coverFaultPhoto = ?4,cf.coverFaultReason = ?5,cf.coverMoney =?6 WHERE cf.coverFaultId =?1")
    public Integer updateCoverFault(Integer coverFaultId, Integer coverFaultLevel, Integer coverFaultType, String coverFaultPhoto, String coverFaultReason, Double coverMoney) throws Exception;

    /**
     * @Description: 信号机故障审核
     * @Author: jiang-weirong
     * @Date: 2017/7/22 18:18
     */
    public List<DTCoverFaultDto> getCoverFault4DT(DTRequestDto dtRequestDto, Integer actorId, Integer orgId) throws Exception;

    public Integer getCoverFaultCount4DT(DTRequestDto dtRequestDto, Integer actorId, Integer orgId) throws Exception;

    /**
     * @Description: 主管审核通过
     * @Author: jiang-weirong
     * @Date: 2017/7/22 18:18
     */
    @Modifying
    @Query(value = "UPDATE CoverFaultEntity  mf SET mf.coverMainCheck= ?1,mf.coverMainCheckerId=?4,mf.coverFeedback=?2,mf.coverMaintenanceStatus=1,mf.coverMainCheckTime=?5 WHERE mf.coverFaultId=?3")
    public Integer coverFaultAudit2Success(Integer isPass, String feedback, Integer machineFaultId, Integer userId, String dateNowStr) throws Exception;

    /**
     * @Description: 主管审核拒绝
     * @Author: jiang-weirong
     * @Date: 2017/7/22 18:19
     */
    @Modifying
    @Query(value = "UPDATE CoverFaultEntity  mf SET mf.coverMainCheck= ?1,mf.coverMainCheckerId=?4,mf.coverFeedback=?2,mf.coverMainCheckTime=?5 WHERE mf.coverFaultId=?3")
    public Integer coverFaultAudit2Fail(Integer isPass, String feedback, Integer machineFaultId, Integer userId, String dateNowStr) throws Exception;

    /**
     * @Description: 故障处理DT
     * @Author: jiang-weirong
     * @Date: 2017/7/22 18:53
     */
    public List<DTCoverFaultDto> getCoverFaultHandle4DT(DTRequestDto dtRequestDto, String actorIds, Integer orgId, Integer machineMaintenanceStatus) throws Exception;

    public Integer getCoverFaultHandleCount4DT(DTRequestDto dtRequestDto, String actorIds, Integer orgId, Integer machineMaintenanceStatus) throws Exception;

    /**
     * @Description: 施工方案
     * @Author: jiang-weirong
     * @Date: 2017/7/15 13:42
     */
    @Modifying
    @Query(value = "UPDATE CoverFaultEntity  mf SET mf.coverMaintenanceContent = ?2 WHERE mf.coverFaultId =?1")
    public Integer addCoverMaintenanceContent(Integer machineFaultId, String machineMaintenanceContent) throws Exception;

    /**
     * @Description: 完成故障维修
     * @Author: jiang-weirong
     * @Date: 2017/7/15 13:43
     */
    @Modifying
    @Query(value = "UPDATE CoverFaultEntity  mf SET mf.coverMaintenancePhoto = ?1,mf.coverMaintainerId=?2,mf.coverMaintenanceTime=?3,mf.coverMaintenanceExplain=?4,mf.coverMaintenanceStatus=2 WHERE mf.coverFaultId =?5")
    public Integer finishCoverMaintenance(String machineMaintenancePhoto, Integer machineMaintainerId, String machineMaintenanceTime, String machineMaintenanceExplain, Integer machineFaultId);

    /**
     * @Description: 获取企业负责人审核情况
     * @Author: jiang-weirong
     * @Date: 2017/7/22 20:35
     */
    @Query(value = "select mf.coverCheck from CoverFaultEntity mf where mf.coverFaultId = ?1")
    public Integer getMachineCheck(Integer coverFaultId) throws Exception;

    /**
     * @Description: 企业负责人审核
     * @Author: jiang-weirong
     * @Date: 2017/7/22 20:36
     */
    @Modifying
    @Query(value = "UPDATE CoverFaultEntity  mf SET mf.coverCheck= ?1,mf.coverCheckerId=?4,mf.coverFeedback=?2,mf.coverCheckTime=?5 WHERE mf.coverFaultId=?3")
    public Integer coverFaultAudit(Integer isPass, String feedback, Integer machineFaultId, Integer userId, String dateNowStr) throws Exception;

    /**
     * @Description: 删除窨井故障
     * @Author: pan-jianan
     * @Date: 2017/8/11 17:20
     */
    @Modifying
    @Query(value = "delete from CoverFaultEntity cf where cf.coverFaultId=?1")
    public Integer deleteCoverFaultByIds(Integer coverFaultId) throws Exception;

    /**
     * @Description: 通过ID删除窨井
     * @author xu_jiaHui
     * @date 2017/8/8 20:47
     */
    @Modifying
    @Query(value = "delete from CoverFaultEntity  where coverId=?1")
    public Integer deleteCoverFault(Integer coverId) throws Exception;


    /**
     * @Description: 判断是否窨井可上报：1可以，0不行
     * @Author: pan-jianan
     * @Date: 2017/8/11 17:20
     */
    @Query(value = "select new com.hualude.roadsign.dto.CoverFaultDto(cf.coverCheck,cf.coverMainCheck,cf.coverMaintenanceStatus) " +
            "from CoverFaultEntity cf where cf.coverId=?1  ORDER BY cf.coverFaultTime DESC ")
    public List<CoverFaultDto> getCoverFaultIsReport(Integer coverId) throws Exception;


    /**
     * @Description: 获取信号机审核列表4App
     * @Author: pan-jianan
     * @Date: 2017/7/17 16:18
     */
    public List<CoverFaultDto> getCoverFaultCheckList4App(Integer orgId, String actorIds, Integer faultStatus,
                                                          Integer count, Integer size) throws Exception;

    /**
     * @Description: app获取信号机故障维修列表
     * @Author: pan-jianan
     * @Date: 2017/7/20 14:22
     */
    public List<CoverFaultDto> getCoverFaultHandleList4App(Integer orgId, Integer maintenanceStatus, String actorIds, Integer count, Integer size) throws Exception;

    /**
     * @Description: app 回执单
     * @Author: pan-jianan
     * @Date: 2017/7/26 15:07
     */
    @Query(value = "select new com.hualude.roadsign.dto.CoverFaultDto(sm.coverId,mf.coverFaultLevel, mf.coverFaultPhoto," +
            "mf.coverFaultType,mf.coverFaultReason,mf.coverMainCheckTime,sm.coverLongitude,sm.coverLatitude,sm.coverProvince," +
            "sm.coverCity, sm.coverArea, sm.coverRoadName, sm.coverStartRoad, sm.coverEndRoad, mf.coverMaintenancePhoto," +
            " mf.coverMaintenanceContent, mf.coverMaintenanceExplain,mf.coverMaintenanceTime,mf.coverFeedback)" +
            " from CoverEntity sm,CoverFaultEntity mf " +
            "WHERE sm.coverId=mf.coverId AND mf.coverFaultId = ?1")
    public CoverFaultDto getMachineReceiptInfo4App(Integer machineFaultId) throws Exception;

    /**
     * @Description: 窨井故障列表4APP
     * @Author: pan-jianan
     * @Date: 2017/8/11 15:38
     */
    public List<CoverFaultDto> getCoverFaultList4App(Integer orgId, String actorIds, Integer faultStatus, Integer count, Integer size) throws Exception;

    /**
     * @Description: 获取推送相关信息
     * @Author: jiang-weirong
     * @Date: 2017/9/11 10:56
     */
    @Query(value = "select new com.hualude.roadsign.dto.CoverFaultDto(coverId, coverOrgId, coverMainOrgId, coverFaultType) from CoverFaultEntity where coverFaultId = ?1")
    public CoverFaultDto getInfoByFaultId(Integer coverFaultId) throws Exception;

    @Modifying
    @Query(value = "UPDATE CoverFaultEntity  lf SET lf.coverCheck= ?1,lf.coverCheckerId=?4,lf.coverFeedback=?2,lf.coverCheckTime=?5,lf.coverMainCheck=?1,lf.coverMainCheckTime=?5,lf.coverMaintenanceStatus=1 WHERE lf.coverFaultId=?3")
    public Integer straightFaultAudit(Integer isPass, String feedback, Integer coverFaultId, Integer userId, String dateNowStr);
}
